from common.connect_db import MyDB


class DataBaseTaxis:

    @staticmethod
    def get_all_not_finish_order(member_id):
        """
        查询未完成的订单
        :param member_id:
        :return:
        """
        my_db = MyDB()
        sql = "select * from life_third_taxis_orders where status "
        result = my_db.execute_select('car', sql)
        if len(result) == 1:
            return result[0]
        else:
            raise ValueError('查询不到上架的车型！')

    @staticmethod
    def get_all_order_by_member_id(member_id, limit=9999):
        """
        获取用户所有订单
        :param member_id:
        :param limit:
        :return: 时间倒序
        """
        my_db = MyDB()
        sql = "select * from life_third_taxis_orders " \
              "where member_id = '%s' order by created_at desc limit %s " % (member_id, limit)
        result = my_db.execute_select('taxis', sql)

        return result

    @staticmethod
    def filter_order_info(**kwargs):
        """
        :param kwargs:
        :return:
        """
        my_db = MyDB()
        # 条件
        condition = ""
        # 排序方式
        order_by = ''
        for k, v in kwargs.items():
            if k == 'order_by':
                order_by = 'order by ' + v
            else:
                condition += ' and ' + str(k) + "='" + str(v) + "'"

        sql = 'select * from life_third_taxis_orders where' + condition[4:] + order_by
        result = my_db.execute_select('taxis', sql)
        if len(result) >= 1:
            return result
        else:
            raise ValueError("没有查询到数据！")

    @staticmethod
    def get_all_child_order(taxis_order):
        """
        根据打车订单号，获得所有三方订单信息
        :param taxis_order: 打车订单号
        :return:
        """
        my_db = MyDB()
        sql = "select * from life_third_taxis_order_relation_platform where taxis_order_sn = '%s' " % taxis_order
        result = my_db.execute_select('taxis', sql)

        return result


if __name__ == '__main__':
    test = DataBaseTaxis()
    test.filter_order_info(member_id=358)
